<template>
  <div>
    <!-- 筛选表单 -->
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model.number="form.age" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="tableData">
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="age" label="年龄" />
      <el-table-column prop="address" label="地址" />
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      form: {
        name: '',
        age: ''
      },
      tableData: [], // 当前页的数据
      total: 0, // 总记录数
      currentPage: 1, // 当前页码
      pageSize: 10 // 每页显示条数
    }
  },
  mounted() {
    this.loadTableData()
  },
  methods: {
    loadTableData() {
      const { name, age } = this.form
      axios.get('/api/users', {
        params: {
          name,
          age,
          page: this.currentPage,
          pageSize: this.pageSize
        }
      }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleSubmit() {
      // 重置分页器
      this.currentPage = 1
      this.loadTableData()
    },
    handleCurrentChange(page) {
      // 切换页码时重新加载数据
      this.currentPage = page
      this.loadTableData()
    }
  }
}
</script>

<style>
/* 可以添加一些自定义样式，如表单/分页器的布局等 */
</style>
